<template>
  <div class="login">
      <el-container>
       <div class="page">
           <div class="management">
               <p>后台管理</p>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
  <el-form-item prop="username">
    <el-input class="input" placeholder="账号" type="text" v-model="ruleForm.username" autocomplete="off">
    <i slot="prefix" class="el-input__icon el-icon-user"></i>
    </el-input>
  </el-form-item>
  <el-form-item prop="password">
    <el-input placeholder="密码" type="password" v-model="ruleForm.password" autocomplete="off">
    <i slot="prefix" class="el-input__icon el-icon-lock"></i>
    </el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" plain @click="submitForm('ruleForm')">登录</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
           </div>
       </div>
      </el-container>
  </div>
</template>

<script>
import {login} from '../api/login.js'
export default {
     data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          if (this.ruleForm.password !== '') {
            this.$refs.ruleForm.validateField('password');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            { validator: validatePass, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            login(this.ruleForm).then(res=>{
            if(res.data.meta.status==200){
              console.log(res);
              sessionStorage.setItem('token', res.data.data.token)
              console.log(sessionStorage.getItem("token"));
              this. $router.push("/home")
               this.$notify({
            title: 'welcome',
           message: '登陆成功',
            type: 'success'
        });
            }else{
              this.$message.error("登陆失败")
            }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
.login{
    height: 100%;
}
.el-container{
    height: 100%;
    background: url(../assets/ym.jpg) center;
    background-size: cover;
}
.page{
    width: 480px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(167, 165, 165,.8);
}
.management{
    width: 100%;
    height: 35px;
    text-align: center;
    font-size: 22px;
    line-height: 20px;
    border-bottom: 1px solid #666;
}
.input{
  margin-top: 25px;
}
.el-form-item{
  margin: 25px auto 20px;
  width: 380px;
}
.el-button{
  margin-top: 5px;
}
</style>